//
//  FeedbackViewController.m
//  dangdang
//
//  Created by air on 15/12/9.
//  Copyright © 2015年 luweijie. All rights reserved.
//

#import "FeedbackViewController.h"

@interface FeedbackViewController ()

@end

@implementation FeedbackViewController

- (void)viewDidLoad {
    [super viewDidLoad];
   
    _feedbackTextView.delegate = self;
    _feedbackTextView.contentInset = UIEdgeInsetsMake(-60, 0, 0, 0);
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"提交" style:UIBarButtonItemStylePlain target:self action:@selector(submit)];
    
    UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(fingerTapped:)];
    
    [self.view addGestureRecognizer:singleTap];
    
    self.title = @"意见反馈";
}

-(void)fingerTapped:(UITapGestureRecognizer *)gestureRecognizer

{
    [self.view endEditing:YES];
}

- (void)submit{
    if ([_feedbackTextView.text isEqual:nil] || [_feedbackTextView.text isEqualToString:@""]) {
            [self alertViewShowWithAlertString:@"内容不能为空"];
            return;
                }
    
    NSString * uid = userObject.userID;
    NSString * content = _feedbackTextView.text;
    NSString * token = userObject.accessToken;
    
    NSMutableDictionary *params = [[NSMutableDictionary alloc]init];
    [params setObject:uid forKey:@"uid"];
    [params setObject:content forKey:@"content"];
    [params setObject:token forKey:@"token"];
    
    [self postRequestWithMethodName:@"user/get_balance" andParams:params andSender:nil];
    [LoadingView showInView:self.view image:nil info:@"通讯中，请稍候..." animated:YES modeled:YES autoHide:NO];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
    if ([text isEqualToString:@"\n"]) {//检测到“完成”
        [textView resignFirstResponder];//释放键盘
        return NO;
    }
    if (_feedbackTextView.text.length==0){//textview长度为0
        if ([text isEqualToString:@""]) {//判断是否为删除键
            _feedbackLabel.hidden=NO;//隐藏文字
        }else{
            _feedbackLabel.hidden=YES;
        }
    }else{//textview长度不为0
        if (_feedbackTextView.text.length==1){//textview长度为1时候
            if ([text isEqualToString:@""]) {//判断是否为删除键
                _feedbackLabel.hidden=NO;
            }else{//不是删除
                _feedbackLabel.hidden=YES;
            }
        }else{//长度不为1时候
            _feedbackLabel.hidden=YES;
        }
    }
    return YES;
}

//网络连接返回数据
- (void)requestSuccessedWithResult:(id)result NSURLSessionTask:(NSURLSessionTask *)task sender:(id)sender{
    
    if ([result isKindOfClass:[NSDictionary class]]) {
        NSString * code = [NSString stringWithFormat:@"%@", [result objectForKey:@"retcode"]];
        if ([code isEqualToString:@"0"]) {
            UIView * view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height)];
            __block int timeout = 2; //倒计时时间
            dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
            dispatch_source_t _timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0,queue);
            dispatch_source_set_timer(_timer,dispatch_walltime(NULL, 0),1.0*NSEC_PER_SEC, 0); //每秒执行
            dispatch_source_set_event_handler(_timer, ^{
                if(timeout<=0){ //倒计时结束，关闭
                    dispatch_source_cancel(_timer);
                    dispatch_async(dispatch_get_main_queue(), ^{
                        //设置界面的按钮显示 根据自己需求设置
                        [view removeFromSuperview];
                        [self.navigationController popToRootViewControllerAnimated:YES];
                    });
                }else{
                    dispatch_async(dispatch_get_main_queue(), ^{
                        //设置界面的按钮显示 根据自己需求设置
                        view.backgroundColor = [UIColor colorWithRed:124/255.0 green:124/255.0 blue:124/255.0 alpha:0.9];
                        [self.view.window addSubview:view];
                        
                        UILabel * label = [[UILabel alloc] initWithFrame:CGRectMake(50, _feedbackTextView.frame.size.height + 104, [UIScreen mainScreen].bounds.size.width - 100, 65)];
                        label.backgroundColor = [UIColor whiteColor];
                        label.text = @"衷心感谢您的宝贵意见!";
                        label.textAlignment = NSTextAlignmentCenter;
                        label.textColor = [UIColor colorWithRed:57/255.0 green:163/255.0 blue:227/255.0 alpha:0.9];
                        label.font = [UIFont boldSystemFontOfSize:18.0f];
                        label.layer.cornerRadius = 10;
                        label.layer.masksToBounds = YES;
                        [view addSubview:label];
                    });
                    timeout--;
                }
            });
            dispatch_resume(_timer);
        }else{
            [self alertViewShowWithAlertString:@"提交失败"];
        }
    }
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
